
$(function(){
    var page =1
    var pageSize =5
  function render (){

    $.ajax({
      url:'/category/querySecondCategoryPaging',
      type:'get',
      data:{
        page:page,
        pageSize:pageSize
      },
      success:function(info){
       console.log(info);
       
        $('tbody').html(template('tmp',info))

          $('#paginator').bootstrapPaginator({
            bootstrapMajorVersion: 3,
            currentPage: page,
            totalPages: Math.ceil(info.total / pageSize),
            onPageClicked: function (a, b, c, p) {
              page = p
              render()
            }
          })
      }

    })



  }
  render()


$('.btn_add').on('click',function(){
   $('#add_modal').modal('show')

  //  请求ajax渲染模态框中下拉菜
  $.ajax({
        type:"get",
        url:"/category/queryTopCategoryPaging",
        data:{
          page:1,
          pageSize:100//希望获取所有
        },
        success:function(info){
          console.log(info)
          $('.dropdown-menu').html(template('tmp1',info))
          
        }
  })
})

//  一级分类的功能 
$('.dropdown-menu').on('click','li',function(){

  var content =$(this).children().html()
  $('.dropdown-text').html(content)
  $('[name=categoryId]').val($(this).data('id'))
  $('form').data('bootstrapValidator').updateStatus('categoryId','VALID')
})


//二级分类图片上传功能

  $('#file').fileupload({
//图片上传后执行的回调函数
   done:function(e,data){
  // console.log(data.result.picAddr)
  $('.img-box img').attr('src',data.result.picAddr)
  //把路径在赋值给input 
  $('[name=brandLogo]').val(data.result.picAddr)
  $('form').data('bootstrapValidator').updateStatus('brandLogo','VALID')
  
   }
})


//表单验证 
$('form').bootstrapValidator({
    //配置的校验的小图标
    feedbackIcons: {
      valid: 'glyphicon glyphicon-thumbs-up',
      invalid: 'glyphicon glyphicon-thumbs-down',
      validating: 'glyphicon glyphicon-refresh'
    },
    //让隐藏表单也参与校验 
    excluded:[],
    fields: {
      brandName: {
        validators: {
          notEmpty: {
            message: '二级分类的名称不能为空'
          }
        }
      },
      categoryId: {
        validators: {
          notEmpty: {
            message: '请选择一个一级分类'
          }
        }
      },
      brandLogo: {
        validators: {
          notEmpty: {
            message: '请上传二级分类的图片'
          }
        }
      }
    }
})

  //给表单注册一个校验成功事件

  $('form').on('success.form.bv',function(e){
    //阻止浏览器默认行为 
    e.preventDefault();
    $.ajax({

      type:"post",
      url:'/category/addSecondCategory',
      data:$('form').serialize(),
      success:function(info){
        console.log("哈哈" +info);
        
        if(info.success){
          page=1
          render()
          //关闭模态框 
        $('#add_modal').modal('hide')

        //重置样式 
        $('.dropdown-text').html('请选择一级分类')
        $('.img-box img').attr('src','../images/none.png')
        }
      }


    })


  })


})